$(function(){
	//添加人员model
	$("#addProjectMember").on('click',function(e){
		showTpModal($("#memberModen"));
		$("#form-field-select-5").chosen(); 
		$('#form_field_select_5_chosen').css({'width':'230px','height':'30px'});
		
		
		return false;
	});
	
	
	//人员选择下拉
	$(".step-content-cfg").on('click',".emp-dropdown-toggle ~ ul > li a",function(e){
		var jVal = $(this).closest(".dropdown-menu").siblings(".emp-dropdown-toggle").children("span");
		jVal.text($(this).children("span").text());
		$("#task-save-form [name='empNo']").attr("value",$(this).children("span").attr("value"));
	});
	
	//人员保存
	$("#memberModen-btn-save").on('click',function(){
		
		if($("#tpModen-form select[name='empNos']").val()==""||$("#tpModen-form select[name='empNos']").val()==null){
			Otod.Msg.info("员工不能为空！");
			return;
		}
		Otod.ajax({
			url: ctx + '/pm/team/saveMember.do',
			data : {
				prjId :$("#globalPrjId").val(),
				empNo :$("#tpModen-form select[name='empNos']").val().join(","),
				roleNo : $("#roleNo").val()
					},
			contentType: "application/x-www-form-urlencoded;",
			successFn : function(data) {
				
//				Otod.Msg.info(data.info);
				$('#memberModen').modal('hide');
				loadMember();
					
			}
		});
	});
	
	//鼠标悬停事件
	$("#projectMember ul").on("mouseover",".removeMember-btn",function(e){
		$(this).css("display","");
		
	});
	//鼠标离开事件
	$("#projectMember ul").on("mouseout",".removeMember-btn",function(e){
		$(this).css("display","none");
		
	});
	
	$("#projectMember ul").on("click",".member-btn-delete",function(e){
		var uuid = $(this).attr("deleteId");
		Otod.Msg.confirm("确定要删除该项目成员吗？", "删除成员",function(){
			Otod.ajax({					
				url : ctx + "/pm/team/delete.do",
				data: uuid,
				successFn:function(data){
					var responseMsg = data.info;
					if(responseMsg.indexOf("成功") < 0 ){
						Otod.Msg.info(responseMsg);
					}else{
						loadMember();
					}
				}
			});
		});
		
		
	});
	
	
	
});

//渲染人员
function renderMember(data){
	var html = "";
	for(var i=0;i<data.length;i++){
		html =  html +  "<li class='block' style='height:65px'><a href='#' onclick='showDeleteButton(\""+data[i].uuid+"\");return false;'><img class='pull-left' height='40px' width='40px' src='"+ctx+"/static/ace/assets/avatars/avatar2.png'/></a>"+data[i].empName+"<br />"+data[i].roleName+"" +
				"<div id='"+data[i].uuid+"' style='display:none' class='removeMember-btn'><div class='col-sm-6'></div><div class=''col-sm-6'><button type='button'  class='member-btn-delete' deleteId='"+data[i].uuid+"'>删除</button></div></div>" +
				"</li>";
	}
	$("#projectMember ul").html(html);
}

function initEmpSelect2(prjId){
	Otod.ajax({
		url: ctx + '/pm/team/findEmpList.do?prjId='+prjId,
		successFn : function(data) {
			var html = new StringBuffer();
			
				html.append("<option value=\"\">").append("请选择").append("</option>");
			
			for(var i=0;i<data.length;i++){
				html.append("<option value=\"").append(data[i].empNo).append("\" ");
				
				html.append(" >").append(data[i].empName).append("</option>");
			}
			
			$("#form-field-select-5").html(html.toString());
		}
	});
	
}

//加载项目人员
function loadMember(){
	Otod.ajax({
		url:ctx+"/pm/team/findByPrjId.do?prjId="+prjId,
		contentType: "application/x-www-form-urlencoded;",
		successFn:function(data){
			renderMember(data);
		}
	});
	
}
//点击显示人员删除按钮
function showDeleteButton(id){
	if(GOBAL_PRJ_ROLE =='30100002'||GOBAL_PRJ_ROLE =='30100007'){//负责人
		
		$("#"+id).css("display","");
	}else{
		return;
		
	}
	
}

